class Email {
  final String id;
  final String subject;
  final String sender;
  final String senderEmail;
  final String body;
  final DateTime date;
  final bool isRead;
  final List<Attachment>? attachments;

  Email({
    required this.id,
    required this.subject,
    required this.sender,
    required this.senderEmail,
    required this.body,
    required this.date,
    this.isRead = false,
    this.attachments,
  });

  factory Email.fromJson(Map<String, dynamic> json) {
    List<Attachment>? attachments;
    if (json['attachments'] != null) {
      attachments = (json['attachments'] as List)
          .map((item) => Attachment.fromJson(item))
          .toList();
    }

    return Email(
      id: json['id'],
      subject: json['subject'] ?? '(无主题)',
      sender: json['sender'] ?? '未知发件人',
      senderEmail: json['senderEmail'] ?? '',
      body: json['body'] ?? '',
      date: DateTime.parse(json['date']),
      isRead: json['isRead'] ?? false,
      attachments: attachments,
    );
  }
}

class Attachment {
  final String name;
  final String type;
  final int size;
  final String? url;

  Attachment({
    required this.name,
    required this.type,
    required this.size,
    this.url,
  });

  factory Attachment.fromJson(Map<String, dynamic> json) {
    return Attachment(
      name: json['name'] ?? '未知文件',
      type: json['type'] ?? 'application/octet-stream',
      size: json['size'] ?? 0,
      url: json['url'],
    );
  }
}